package com.ruifu.act.listener;

import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.delegate.event.ActivitiEvent;
import org.activiti.engine.delegate.event.ActivitiEventListener;
import org.activiti.engine.delegate.event.ActivitiEventType;
import org.activiti.engine.delegate.event.impl.ActivitiEntityEventImpl;
import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
import org.activiti.engine.impl.persistence.entity.TaskEntity;
import org.springframework.stereotype.Component;

@Component("processEventListener")
@Slf4j
public class ProcessEventListener implements ActivitiEventListener {

    @Override
    public void onEvent(ActivitiEvent event) {
        ActivitiEventType eventType = event.getType();

        ActivitiEntityEventImpl eventImpl;
        ExecutionEntity executionEntity;
        TaskEntity taskEntity;

        //ExecutionEntity executionEntity = (ExecutionEntity) eventImpl.getEntity();
        //TaskEntity taskEntity = (TaskEntity) eventImpl.getEntity();

        switch (eventType) {
            case PROCESS_STARTED:
                eventImpl = (ActivitiEntityEventImpl) event;
                executionEntity = (ExecutionEntity) eventImpl.getEntity();
                log.info("[{}] 流程启动 \t =====> 流程实例ID:{},对应的业务BusinessKey:{}", eventType, event.getProcessInstanceId(), executionEntity.getBusinessKey());
                break;
            case PROCESS_COMPLETED:
                eventImpl = (ActivitiEntityEventImpl) event;
                executionEntity = (ExecutionEntity) eventImpl.getEntity();
                log.info("[{}] 流程结束 \t =====> 流程实例ID:{},对应的业务BusinessKey:({})", eventType, event.getProcessInstanceId(), executionEntity.getBusinessKey());
                break;
            case ACTIVITY_STARTED:
                log.info("[{}] 一个执行开始 \t =====> 执行实例ID:{}", eventType, event.getExecutionId());
                break;
            case ACTIVITY_COMPLETED:
                log.info("[{}] 一个执行结束 \t =====> 执行实例ID:{}", eventType, event.getExecutionId());
                break;
            case TASK_ASSIGNED:
                eventImpl = (ActivitiEntityEventImpl) event;
                taskEntity = (TaskEntity) eventImpl.getEntity();
                log.info("[{}] 一个任务被指派了给了{} \t =====> 任务实例ID:{}", eventType, taskEntity.getAssignee(), taskEntity.getId());
                break;
            case TASK_CREATED:
                eventImpl = (ActivitiEntityEventImpl) event;
                taskEntity = (TaskEntity) eventImpl.getEntity();
                log.info("[{}] 一个节点{},对应的任务开始 \t =====> 任务实例ID:{}", eventType, taskEntity.getTaskDefinitionKey(), taskEntity.getId());
                break;
            case TASK_COMPLETED:
                eventImpl = (ActivitiEntityEventImpl) event;
                taskEntity = (TaskEntity) eventImpl.getEntity();
                log.info("[{}] 一个节点{},对应的任务结束 \t =====> 任务实例ID:{}", eventType, taskEntity.getTaskDefinitionKey(), taskEntity.getId());
                break;

        }
    }

    @Override
    public boolean isFailOnException() {
        return false;
    }
}